<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\DocService;
use Illuminate\Http\Request;

class DocController extends BaseController
{
    //首页

    public function __construct()
    {
    }

    //列表
    public function leftMenu()
    {
        $res = (new DocService())->leftMenu();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = (new DocService())->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //新增
    public function add(Request $request)
    {
        $id = $request->post('id');
        $pas['title'] = $request->post('title', '');
        $pas['uri'] = $request->post('uri', '');
        $pas['reqbody'] = $request->post('req', '');
        $pas['backbody'] = $request->post('back', '');
        if (!$pas['title']) {
            return $this->toJson(-2, '接口名必须的');
        }
        if (!$pas['uri']) {
            return $this->toJson(-2, '接口地址必须的');
        }
        if ($id) {
            $res = (new DocService())->modify($id, $pas);
        } else {
            $res = (new DocService())->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}